#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Fix admin account
"""

import os
import sys
import django

# Setup Django
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'TradingPlatform_Server.settings')
django.setup()

from Server.models import User
from django.contrib.auth.hashers import make_password, check_password

def fix_admin():
    print("=" * 60)
    print("Admin Account Fix")
    print("=" * 60)
    
    admin_phone = '13800138000'
    
    try:
        admin = User.objects.get(phone=admin_phone)
        print("\nFound admin account:")
        print("  ID:", admin.id)
        print("  Username:", admin.username)
        print("  Phone:", admin.phone)
        print("  Is Admin:", admin.is_admin)
        
        # Reset password
        print("\nResetting password to 'admin123'...")
        admin.password = make_password('admin123')
        admin.is_admin = True
        admin.save()
        
        # Verify
        admin.refresh_from_db()
        if check_password('admin123', admin.password):
            print("SUCCESS: Password verified!")
        else:
            print("ERROR: Password verification failed!")
        
        print("\n" + "=" * 60)
        print("Admin account fixed!")
        print("=" * 60)
        print("\nLogin credentials:")
        print("  Phone: 13800138000")
        print("  Password: admin123")
        
    except User.DoesNotExist:
        print("\nAdmin not found, creating...")
        admin = User.objects.create(
            id='admin001',
            username='Admin',
            phone=admin_phone,
            password=make_password('admin123'),
            is_admin=True,
            balance=0.0
        )
        print("SUCCESS: Admin created!")
        print("  Phone: 13800138000")
        print("  Password: admin123")
    
    except Exception as e:
        print("\nERROR:", str(e))
        import traceback
        traceback.print_exc()

if __name__ == '__main__':
    fix_admin()
    raw_input("\nPress Enter to exit...")

